<?php
if(defined('IZ_ACCNAME')) {
     include_once($_SESSION['IZ_CALPATH'].'includes/data/settings.php');
     $error = '';
     $warning = '';
     if(isset($_POST['setusercust'])) {
        $_SESSION['IZ_LANG'] = $_POST['IZ_LANG'];
        $_SESSION['IZ_CURTHEME'] = $_POST['IZ_CURTHEME'];
        $_SESSION['IZ_MVIEWDAYSIZE'] = $_POST['IZ_MVIEWDAYSIZE'];
        $_SESSION['IZ_FIRSTDAY'] = $_POST['IZ_FIRSTDAY'];
        $_SESSION['fontsize'] = $_POST['fontsize'];
        $_SESSION['defaultstatus'] = $_POST['defaultstatus'];
        $_SESSION['showall'] = intval($_POST['showall']);
        if($_SESSION['showall']) {
          $_SESSION['publicstatus'] = " OR status = 'Public'";
        } else {
          $_SESSION['publicstatus'] = '';
        }
        if($_POST['dformat'] == 't1') {
          $_SESSION['dateformat'] = $_POST['dformatpreset1'];
        } else {
          $_SESSION['dateformat'] = $_POST['dformatpreset2'];
        }
        $_SESSION['defaultcategoryevents'] = $_POST['defaultcatevent'];
        $_SESSION['defaultcategorytasks'] = $_POST['defaultcattodo'];
        $_SESSION['pageafter'] = $_POST['pageafter'];
     } elseif($_GET['act'] == 'logout') {
        $_SESSION = array();
     }
     if(!isset($_SESSION['IZ_CALURL'])) {
       $sql = 'SELECT * FROM settings';
       if(IZ_CALMODE == 'mysql') {
         if(function_exists('mysqli_connect')) {
           $_SESSION['MYSQLi'] = true;
           $dblink = mysqli_connect($dbhost, $dbuser, $dbpass,$dbdatabase);
           if(mysqli_connect_errno()) {
              echo 'Can\'t connect to MySQL Server as user: ',$dbuser,',Errorcode: ',mysqli_connect_error(),"\n";
              die();
           }
           $res = $dblink->query($sql);
           if($res->num_rows > 0) {
             while($settingrow = $res->fetch_object()) {
               if(!in_array($settingrow->name,array('IZ_LANG','IZ_MVIEWDAYSIZE','IZ_FIRSTDAY','IZ_CURTHEME'))) {
                 $_SESSION[$settingrow->name] = $settingrow->setval;
               } else {
                 if(!isset($_SESSION[$settingrow->name])) {
                   $_SESSION[$settingrow->name] = $settingrow->setval;
                 } elseif(isset($_POST['setusercust'])) {
                   $_SESSION[$settingrow->name] = $_POST[$settingrow->name];
                 }
               }
             }
           }
         } else {
           $_SESSION['MYSQLi'] = false;
           $dblink = mysql_connect($dbhost, $dbuser , $dbpass) or $error .= '<p><strong>'.ERR_DBCONNECTFAIL.'</strong>&#58;<br />'.mysql_error().'</p>';
           @mysql_select_db($dbdatabase, $dblink);
           $sqlver = ereg_replace("[^0-9\.]", "", mysql_get_server_info());
           $qry = mysql_query($sql);
           while($settingrow = mysql_fetch_object($qry)) {
             if(!in_array($settingrow->name,array('IZ_LANG','IZ_MVIEWDAYSIZE','IZ_FIRSTDAY','IZ_CURTHEME'))) {
               $_SESSION[$settingrow->name] = $settingrow->setval;
             } else {
               if(!isset($_SESSION[$settingrow->name])) {
                 $_SESSION[$settingrow->name] = $settingrow->setval;
               } elseif(isset($_POST['setusercust'])) {
                 $_SESSION[$settingrow->name] = $_POST[$settingrow->name];
               }
             }
           }
         }
       } elseif(IZ_CALMODE == 'postgre') {
         $sql = 'SELECT * FROM "settings"';
         $pgconn = pg_connect("host=$dbhost dbname=$dbdatabase user=$dbuser password=$dbpass") or $error .= '<p><strong>'.ERR_DBCONNECTFAIL.'</strong>&#58;<br />'.pg_last_error().'</p>';
         $qry = pg_query($sql);
         while($settingrow = pg_fetch_object($qry)) {
              if(!in_array($settingrow->name,array('IZ_LANG','IZ_MVIEWDAYSIZE','IZ_FIRSTDAY','IZ_CURTHEME'))) {
                $_SESSION[$settingrow->name] = $settingrow->setval;
              } else {
                if(!isset($_SESSION[$settingrow->name])) {
                  $_SESSION[$settingrow->name] = $settingrow->setval;
                } elseif(isset($_POST['setusercust'])) {
                  $_SESSION[$settingrow->name] = $_POST[$settingrow->name];
                }
              }
         }
       } elseif(IZ_CALMODE == 'msq') {
         $dblink = mssql_connect($dbhost, $dbuser, $dbpass);
         mssql_select_db($dbdatabase, $dblink) or die('&#35;447&#58; '.ERR_DBCONNECTFAIL.'&#58; '.mssql_get_last_message());
         $qry = mssql_query($sql);
         while($settingrow = mssql_fetch_object($qry)) {
              if(!in_array($settingrow->name,array('IZ_LANG','IZ_MVIEWDAYSIZE','IZ_FIRSTDAY','IZ_CURTHEME'))) {
                $_SESSION[$settingrow->name] = $settingrow->setval;
              } else {
                if(!isset($_SESSION[$settingrow->name])) {
                  $_SESSION[$settingrow->name] = $settingrow->setval;
                } elseif(isset($_POST['setusercust'])) {
                  $_SESSION[$settingrow->name] = $_POST[$settingrow->name];
                }
              }
         }
       }
       $numqs++;
       if($_SESSION['IZ_LANG'] == 'en') { // default language
         detectlanguage();
       }
       if(!isset($_SESSION['IZ_CALURL'])) {
         include($_SESSION['IZ_CALPATH'].'includes/debug.php');
       }
     }
     include($_SESSION['IZ_CALPATH'].'includes/lang/'.$_SESSION['IZ_LANG'].'_core.php');
     $servertime = mktime();
     $curtime = $servertime + ($_SESSION['IZ_TIMEOFFSET']*3600);
     $curDay = date('j',$curtime);
     $curMonth = date('n',$curtime);
     $curYear = date('Y',$curtime);
     include($_SESSION['IZ_CALPATH'].'includes/data/icons.php');
     define('IZ_MINVER','3.0.0');
     define('IZ_CALVERSION','3.5.2');     
    
    define('NV_SETTINGS', 1);
    define('NV_EVMGR',    NV_SETTINGS<<1);
    define('NV_RSS',      NV_SETTINGS<<2);
    define('NV_ICAL',     NV_SETTINGS<<3);
    define('NV_SEARCH',   NV_SETTINGS<<4);
    define('NV_REPORTS',  NV_SETTINGS<<5);
    define('NV_PRINT',    NV_SETTINGS<<6);
    define('NV_HELP',     NV_SETTINGS<<7);
    define('NV_DAY',      NV_SETTINGS<<8);
    define('NV_WEEK',     NV_SETTINGS<<9);
    define('NV_MONTH',    NV_SETTINGS<<10);
    define('NV_EXTMONTH', NV_SETTINGS<<11);
    define('NV_YEAR',     NV_SETTINGS<<12);

    define('OPT_ENDDATE',         1);
    define('OPT_ATTENDEES',       OPT_ENDDATE<<1);
    define('OPT_VENUE',           OPT_ENDDATE<<2);
    define('OPT_GOOGLE',          OPT_ENDDATE<<3);
    define('OPT_ICON',            OPT_ENDDATE<<4);
    define('OPT_CATEGORY',        OPT_ENDDATE<<5);
    define('OPT_RECURRENCE',      OPT_ENDDATE<<6);
    define('OPT_FILTERS',         OPT_ENDDATE<<7);
    define('OPT_GOTO',            OPT_ENDDATE<<8);
    define('OPT_TODO',            OPT_ENDDATE<<9);
    define('OPT_REG',             OPT_ENDDATE<<10);
    define('OPT_GOOGLENEWWINDOW', OPT_ENDDATE<<11);
/*$opt = OPT_ENDDATE |OPT_ATTENDEES |OPT_VENUE|OPT_GOOGLE | OPT_ICON| OPT_CATEGORY|OPT_RECURRENCE|OPT_FILTERS|OPT_GOTO|OPT_TODO|OPT_REG|OPT_GOOGLENEWWINDOW;
echo $opt,'<br>';/*
$perms = NV_SETTINGS |NV_EVMGR |NV_RSS|NV_ICAL | NV_SEARCH| NV_REPORTS|NV_PRINT |NV_HELP| NV_DAY | NV_WEEK | NV_MONTH |NV_EXTMONTH| NV_YEAR;
echo $perms.':<br>',
$perms = NV_SETTINGS |NV_ICAL |NV_SEARCH|NV_DAY | NV_MONTH;
echo $perms.':<br>';*/
} else {
  // Directly accessed
  header('Location: ../');
} ?>